/**
 * @format
 * @Author: TC
 * @Date: 2022-03-04 18:01:37
 * @Last Modified by: TC
 * @Last Modified time: 2023-03-25 19:23:38
 */

import {
    _decorator,
    Asset,
    assetManager,
    director,
    instantiate,
    isValid,
    Node,
    Prefab,
    Sprite,
    tween,
    Tween,
    UITransform,
    v3,
    view,
} from "cc";
import { app } from "../../App";
import { mFrameEnumConst } from "../../Const/FrameEnumConst";
import { mGlobalData } from "../../Data/GlobalData";
import { SingleBase } from "../../Interface/SingleBase";

const { ccclass, property } = _decorator;

export class LoadingWindow extends SingleBase {
    private _loadingNode: Node = null;

    private barSprite: Sprite = null;

    private type: number = 1;

    public static get instance(): LoadingWindow {
        return this._instance ? (this._instance as LoadingWindow) : (this._instance = new LoadingWindow());
    }

    //0--场景1--资源
    onStart(type: number = 0): void {
        this._loadingNode = instantiate(mGlobalData.prefabList.preLoadingWindow);
        this.onInit();
        this.type = type;
        app.sceneMgr.getCurrCanvasNode().addChild(this._loadingNode);
        this._loadingNode.setSiblingIndex(mFrameEnumConst.Z_INDEX.TOP);
        this.barSprite.node.setScale(v3(0, 1, 1));
        let winSize = view.getVisibleSize();
        this._loadingNode.setPosition(v3(winSize.width / 2, winSize.height / 2, 1));
        director.getScene().on(Node.EventType.SIZE_CHANGED, this.onResize, this);
        this.onResize();
    }

    private onResize(): void {
        let winSize = view.getVisibleSize();
        var dht: number = view.getDesignResolutionSize().height;
        var ht: number = winSize.height;
        var scale = 1;
        if (ht < dht) {
            //实际屏幕偏扁时进行缩放
            scale = ht / dht;
        }
        this._loadingNode.scale = v3(scale, scale, 1);
        this._loadingNode.setPosition(v3(winSize.width / 2, winSize.height / 2, 0));
    }

    onProgress(curr: number, total: number): void {
        var per: number = 0 != total ? curr / total : 0;
        this.barSprite.node.scale = v3(0 == this.type ? per * 0.1 : 0.1 + per * 0.9, 1, 1);
    }

    onComplete(): void {
        if (this._loadingNode && isValid(this._loadingNode, true)) {
            this._loadingNode.destroy();
            this._loadingNode = null;
        }
    }

    onInit(): void {
        this.barSprite = this._loadingNode.getChildByName("loadingBar").getComponent(Sprite);
        this.barSprite.node.scale = v3(0, 1, 1);
    }

    // public startLoadGameScene(): void {
    //     this.onStart();
    //     this.barSprite.node.scale = v3(1, 1, 1);
    //     this.barSprite.node.getComponent(UITransform).width = 0;

    //     assetManager.loadBundle(mBundleConst.Game, () => {

    //         tween(this.barSprite.node.getComponent(UITransform)).to(10, { width: 219 }).start();

    //         this.loadDynamicRes();
    //     });

    // }

    // /**
    //  * 加载动态资源
    //  */
    // private loadDynamicRes(): void {
    //     Promise.all([
    //         this.promiseLoadDir("Role"),
    //     ]).then(() => {
    //         Tween.stopAllByTarget(this.barSprite.node.getComponent(UITransform));
    //         tween(this.barSprite.node.getComponent(UITransform)).to(0.5, { width: 300 }).start();

    //         director.preloadScene(SceneAndWindowConst.GAME_SCENE, () => {
    //             tween(this.barSprite.node.getComponent(UITransform)).to(0.5, { width: 438 }).call(
    //                 () => {
    //                     director.loadScene(SceneAndWindowConst.GAME_SCENE);
    //                 }
    //             ).start();
    //         });
    //     })

    // }

    // private promiseLoadDir(dir: string): Promise<Asset[]> {
    //     return new Promise((resolve, reject) => {
    //         assetManager.getBundle(mBundleConst.Game).loadDir(dir, (err, data: Asset[]) => {
    //             if (!err) {
    //                 resolve(data);
    //             }
    //         });
    //     });
    // }
}
